import { createRouter, createWebHashHistory } from 'vue-router'
import Login from "../views/login/Login"
import Home from '../views/home/Home'
import Welcome from "../views/home/Welcome";
import UserList from "../views/user/UserList";
//权限管理
import RoleList from "../views/rightManage/RoleList";
import RightList from "../views/rightManage/RightList";
//订单管理
import OrderList from "../views/orderManage/OrderList";
//商品管理
import GoodCategory from "../views/goodsManage/GoodCategory";
import GoodList from "../views/goodsManage/GoodList";
import Parameters from "../views/goodsManage/Parameters";
import AddGood from '../views/goodsManage/addGood';
//数据管理
import Report from "../views/DataManage/Report";

const routes = [
  { path: "/", redirect: "/login" },
  { path: "/login", component: Login },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children:[
      {path:'/welcome', component: Welcome },
      {path: '/users',component: UserList },

      {path: '/roles',component: RoleList },
      {path: '/rights', component: RightList },

      {path: '/goods', component: GoodList },
      { path: '/goods/add',component: AddGood },
      {path: '/params', component: Parameters },
      {path: '/categories', component: GoodCategory},

      {path: '/orders', component: OrderList },

      {path: '/reports', component: Report },
    ]
  },

]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

//添加beforeEach导航守卫
router.beforeEach((to,from,next)=>{
  if(to.path === '/login'){
    next();
  }else{
    const token = sessionStorage.getItem("Token");
    if(token){
      next();
    }else{
      next("/login");
    }
  }
})

export default router
